package com.flexdms.i18n;

import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Logger;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;



public class DefaultResourceImpl extends ResourceBundle
{

	public static final Logger logger=Logger.getLogger(DefaultResourceImpl.class.getName());
	private final Ehcache cache;

	
	public DefaultResourceImpl() 
	{
		super();
		try
		{
			String cacheName=getClass().getName();
			cache=CacheManager.getInstance().addCacheIfAbsent(cacheName);
			msgs=I18NMessage.getDatabaseI18nMessage().loadMessages(getColumnName());
		} catch (SQLException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	
	Map<String, String> msgs;
	

	

	

	public Map<String, String> getMsgs()
	{
		return msgs;
	}

	public void setMsgs(Map<String, String> msgs)
	{
		this.msgs = msgs;
	}

	public String getColumnName()
	{
		String name=this.getClass().getSimpleName();
		return name.substring(DBMsgs.class.getSimpleName().length()+1);
	}

	
	public static boolean isNumeric(String str)
	{
	  NumberFormat formatter = NumberFormat.getInstance();
	  ParsePosition pos = new ParsePosition(0);
	  formatter.parse(str, pos);
	  return str.length() == pos.getIndex();
	}
	
	@Override
	protected Object handleGetObject(String key)
	{
		if (isNumeric(key))
		{
			return key;
		}
		if (msgs.containsKey(key.toLowerCase()))
		{
			String valueString=msgs.get(key.toLowerCase());
			if (valueString!=null)
			{
				
				return valueString;
			}
		}
		
		I18NMessage i18nMessage=I18NMessage.getDatabaseI18nMessage();
		if (i18nMessage.isAutoTranslate())
		{
			try
			{
				//do not insert
				//only defaultLocale do the insertion
				String result=_translate(key);
				//cache result;
				msgs.put(key, result);
				return result;
			} catch (Exception e)
			{
				logger.info(e.getMessage());
			}
		}
		
		return key;
	}
	

	@Override
	protected Set<String> handleKeySet()
	{
		return msgs.keySet();
	}

	@Override
	public Enumeration<String> getKeys()
	{
		return Collections.enumeration(msgs.keySet());
	}
	
	public String getTempTranslation(String key)
	{
		if (msgs.containsKey(key))
		{
			return msgs.get(key);
		}
		if (cache.get(key)!=null)
		{
			return (String) cache.get(key).getObjectValue();
		} else {
			I18NMessage i18nMessage=I18NMessage.getDatabaseI18nMessage();
			if (i18nMessage.isAutoTranslate())
			{
				try
				{
					String result = _translate(key);
					cache.put(new Element(key, result));
					return result;
				} catch (Exception e)
				{
					logger.info(e.getMessage());
				}
			
			}
			return key;
		}
	}
	
	protected String _translate(String msg) throws Exception
	{
		
		return I18NMessage.getDatabaseI18nMessage().getTranslator().translate(msg, getLocale());
	}


	public Ehcache getCache()
	{
		return cache;
	}
}
